package com.cvmars.jianyou.module.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cvmars.jianyou.R;
import com.cvmars.jianyou.api.api.Api;
import com.cvmars.jianyou.api.api.HttpUtils;
import com.cvmars.jianyou.api.api.SimpleSubscriber;
import com.cvmars.jianyou.api.model.HttpResult;
import com.cvmars.jianyou.api.model.UserMode;
import com.cvmars.jianyou.model.UserModel;
import com.cvmars.jianyou.ui.CircleImageView;
import com.cvmars.jianyou.utils.ImageUtils;
import com.cvmars.jianyou.utils.ToastUtils;
import com.cvmars.jianyou.utils.UtilHelper;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by hehaifeng on 2018/12/16.
 */

public class UserNearByAdapter extends BaseQuickAdapter<UserModel, BaseViewHolder> {

    Context mContext;

    public UserNearByAdapter(Context context, int layoutResId, @Nullable List<UserModel> data) {
        super(layoutResId, data);
        mContext = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, final UserModel item) {


        TextView txtGuanzhu = helper.getView(R.id.txt_guanzhu);
        ImageView iv_vip = helper.getView(R.id.iv_vip);

//        if (item.relation_status == 2) {
//
//            txtGuanzhu.setText("互相关注");
//        } else if (item.relation_status == 1) {
//
//            txtGuanzhu.setText("已关注");
//
//        } else if (item.relation_status == -1) {
//
//            txtGuanzhu.setText("关注");
//        }

        iv_vip.setVisibility(item.is_rut ? View.VISIBLE : View.GONE);

        helper.setText(R.id.txt_name, item.name)
                .setText(R.id.txt_guanzhu, item.address_home)
                .setText(R.id.txt_address, getDistance(item.distance))
                .setText(R.id.txt_time, UtilHelper.forumFriendlyTime(item.last_request_at));

        TextView view = helper.getView(R.id.txt_intro);

        if (view != null) {
            view.setText(item.intro);
        }

        CircleImageView image = helper.getView(R.id.iv_avator);
        ImageView sex = helper.getView(R.id.iv_sex);

        if (item.gender == 1) {

            sex.setImageResource(R.drawable.ic_male);
        } else {

            sex.setImageResource(R.drawable.ic_women);
        }

        ImageUtils.loadImage(mContext, item.avatar_url, image);

//        txtGuanzhu.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//
//                if (item.relation_status == -1) {
//
//                    onFollow(item, "1");
//                } else {
//                    onUnFollow(item);
//                }
//            }
//        });
    }


    private String getDistance(double distance){

        if(distance > 1000){

           double s =  distance / 1000;
            BigDecimal b = new BigDecimal(s);
            Double d = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
           return d + "Km";
        }

        BigDecimal b = new BigDecimal(distance);
        Double d = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return d + "m";
    }

    private void onFollow(final UserModel model, final String status) {

        HttpUtils.getInstance().toSubscribe(Api.getInstance().postUserFollow(
                model.id, status)
                , new SimpleSubscriber<HttpResult<UserMode>>() {
                    @Override
                    protected void _onNext(HttpResult<UserMode> o) {

                        ToastUtils.show(o.getMsg());
                        model.relation_status = 1;
                        UserNearByAdapter.this.notifyDataSetChanged();
                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });
    }


    private void onUnFollow(final UserModel customid) {

        HttpUtils.getInstance().toSubscribe(Api.getInstance().deleteUserFollow(
                customid.id)
                , new SimpleSubscriber<HttpResult<UserMode>>() {
                    @Override
                    protected void _onNext(HttpResult<UserMode> o) {

                        ToastUtils.show(o.getMsg());
                        customid.relation_status = -1;
                        UserNearByAdapter.this.notifyDataSetChanged();
                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });
    }
}
